<?php
$userItem = $this->userItem;
$allMyRoles = $userItem->getAllRole();
$allRole = $this->allRole;
?>


<div class="ad_title_ctn">
    <h2>Sửa user <?php echo $userItem->username?></h2>
</div>
<div class="ad_content_ctn">
    <div class="control_ctn">
        <a id='btnSaveUser' class="k-button k-button-icontext k-grid-edit"><span>Lưu</span></a>
        <a id='btnResetPass' class="k-button k-button-icontext k-grid-edit"><span>Reset Password</span></a>

    </div>
    <fieldset style="margin: 10px 5px">
        <legend>Thông tin cơ bản</legend>
        <div id="basic_info_ctn">
            <table style="width:100%;" cellpadding="0" cellspacing="0">
                <tr>
                    <td style=" vertical-align: top;">
                        <div>

                            <div style="display: block;">
                                <div class="label_150 left">
                                    Tên (*):
                                </div>
                                <div class="left">
                                    <input value="<?php echo $userItem->name ?>" id="txtName" name="txtName" style="width:250px" class="k-textbox" required placeholder="Họ và tên đầy đủ, bắt buộc" validationMessage="Chưa nhập tên người dùng">
                                    <span class="k-invalid-msg" data-for="txtName"></span>
                                </div>
                                <div class="clear"></div>
                            </div>
                            <div style="display: block;">
                                <div class="label_150 left">
                                    Tên đăng nhập(*):
                                </div>
                                <div class="left">
                                    <input value="<?php echo $userItem->username ?>" id="txtUsername" name="txtUsername" style="width:250px" class="k-textbox" required placeholder="Tên đăng nhập, bắt buộc" validationMessage="Chưa nhập tên đăng nhập ">
                                    <span class="k-invalid-msg" data-for="txtUsername"></span>
                                </div>
                                <div class="clear"></div>
                            </div>
                            <div style="display: block;">
                                <div class="label_150 left">
                                    Email:
                                </div>
                                <div class="left">
                                    <input value="<?php echo $userItem->email ?>" id="txtEmail" type="email" name="txtEmail" style="width:250px" class="k-textbox"  placeholder="hoanglan@exam.net" data-email-msg="Email sai định dạng">
                                    <span class="k-invalid-msg" data-for="txtEmail"></span>
                                </div>
                                <div class="clear"></div>
                            </div>
                            <div style="display: block;">
                                <div class="label_150 left">
                                    Phone number:
                                </div>
                                <div class="left">
                                    <input value="<?php echo $userItem->phone_number ?>" id="txtPhone" name="txtPhone" style="width:250px" class="k-textbox"  placeholder="0987654321" >
                                    <span class="k-invalid-msg" data-for="txtPhone"></span>
                                </div>
                                <div class="clear"></div>
                            </div>

                            <div style="display: block;">
                                <div class="label_150 left">
                                    Chọn role:
                                </div>
                                <div class="left" style="padding: 8px 0px">
                                    <?php


                                    foreach ($allRole as $aRole) {
                                        $checked = '';
                                        foreach ($allMyRoles as $aMyRole) {
                                            if($aMyRole->id == $aRole->id)
                                                $checked = 'checked';
                                        }

                                        echo '<span>'.$aRole->name.'</span>&nbsp;<input '.$checked.' type="checkbox" name="chkRole" value="'.$aRole->id.'">&nbsp;&nbsp;&nbsp;';
                                    }

                                    ?>
                                </div>
                                <div class="clear"></div>
                            </div>
                            <div style="display: block;">
                                <div class="label_150 left">
                                    Active:
                                </div>
                                <div class="left" style="padding: 8px 0px">
                                    <input type="checkbox" id="chkActive" <?php echo $userItem->active==1?'checked':'' ?>>
                                </div>
                                <div class="clear"></div>
                            </div>

                            <div style="display: block;">
                                <div class="label_150 left">
                                    Log hoạt động:
                                </div>
                                <div class="left" style="padding: 8px 0px">
                                    <input type="checkbox" id="chkLog" <?php echo $userItem->is_log==1?'checked':'' ?>>
                                </div>
                                <div class="clear"></div>
                            </div>


                        </div>
                    </td>

                </tr>
            </table>
        </div>
    </fieldset>


</div>
<script type="text/javascript">
    var validator;
    $(document).ready(function(){
        var userId = <?php echo  $userItem->id ?>;
        validator = $("#basic_info_ctn").kendoValidator().data("kendoValidator");

        function validate(){
            return validator.validate();
        }

        function saveUser(successSave){
            if(!validate())
                return;

            name = $("#txtName").val();
            username = $("#txtUsername").val();
            email = $("#txtEmail").val();
            phone = $("#txtPhone").val();


            accessRoles = [];
            $(":input[name='chkRole']").each(function() {
                if($(this).is(':checked'))
                    accessRoles.push(this.value);
            });
            active = $('#chkActive').is(':checked')?1:0;
            isLog = $('#chkLog').is(':checked')?1:0;


            $.post('/admin/user/save',{userId:userId,
                name:name, username:username, email:email,
                phone:phone,
                accessRoles:accessRoles,active:active, isLog:isLog
            },function(result){
                if (result.success){
                    successSave();
//
                }
                else{
                    $('#error_ctn').removeClass('blue_text').html(result.msg);
                    alert(result.msg);
                }
            },'json');
        }
        $('#btnSaveUser').click(function(){
            saveOnly();
        })
        $('#btnResetPass').click(function(){
            resetPassword();
        })

        function resetPassword(){
            $.post('/admin/user/reset-pass',{userId:userId
            },function(result){
                if (result.success){

                    $('#error_ctn').addClass('blue_text').html('Password mới của user <?php echo $userItem->username ?> là: '+ result.password);
                    alert('Password mới của user <?php echo $userItem->username ?> là: '+ result.password);
                }
                else{
                    $('#error_ctn').removeClass('blue_text').html(result.msg);
                    alert(result.msg);
                }
            },'json');
        }

        function saveOnly(){
            saveUser(function(){
                window.location="/admin/user";
            })
        }
    });
</script>